package com.bitcat.lesson.wechatapp;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;


import android.os.Bundle;
import android.view.MenuItem;

import com.bitcat.lesson.wechatapp.fragment.AddressBookFragment;
import com.bitcat.lesson.wechatapp.fragment.DiscoveryFragment;
import com.bitcat.lesson.wechatapp.fragment.HomeFragment;
import com.bitcat.lesson.wechatapp.fragment.MeFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    List<Fragment> list;

    BottomNavigationView bottomNavigation;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bottomNavigation = findViewById(R.id.bottomNavigation);

        list = new ArrayList<>();
        list.add(new HomeFragment());
        list.add(new AddressBookFragment());
        list.add(new DiscoveryFragment());
        list.add(new MeFragment());

        showFragment(list.get(0));


        //
       bottomNavigation.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
           @Override
           public boolean onNavigationItemSelected(@NonNull MenuItem item) {
               switch (item.getItemId()){
                   case R.id.menu_home:
                       showFragment(list.get(0));
                       break;
                   case R.id.menu_address_book:
                       showFragment(list.get(1));
                       break;
                   case R.id.menu_discovery:
                       showFragment(list.get(2));
                       break;
                   case R.id.menu_me:
                       showFragment(list.get(3));
                       break;
               }
               return true;
           }
       });
    }


    private void showFragment(Fragment fragment){
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.container,fragment);
        ft.commit();
    }
}